﻿Imports BUS
Imports DTO
Public Class UserControlPhieuGuiTien

    Private Sub UserControlPhieuGuiTien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Load ma so len combobox
        Dim stkBUS As New SoTietKiemBUS()
        Dim dsstk As New List(Of SoTietKiemDTO)()
        dsstk = stkBUS.LaySoTietKiemDuocGoiTien()
        Dim stkDTO As New SoTietKiemDTO()

        cbbSoTietKiem.DataSource = dsstk
        cbbSoTietKiem.DisplayMember = "StrMaSo"
        cbbSoTietKiem.ValueMember = "MaSo"

        'Load khach hang len textbox
        Dim maso As Integer = cbbSoTietKiem.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()
        Dim pgtBUS As New PhieuGoiTienBUS()
        txtMaPhieuGoi.Text = (pgtBUS.LayMaPhieuGoiMax() + 1).ToString()

    End Sub

  
    Private Sub cbbSoTietKiem_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim maso As Integer = cbbSoTietKiem.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()
    End Sub

    Private Sub btnTiepNhan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTiepNhan.Click
        Dim temp As Double = 0
        If Double.TryParse(txtSoTienGoi.Text, temp) = False Then
            MessageBox.Show("Số tiền gởi không hợp lệ")
            Return
        End If
        Dim pgtBUS As New PhieuGoiTienBUS()
        Dim pgtDTO As New PhieuGoiTienDTO()
        pgtDTO.MaSo = cbbSoTietKiem.SelectedValue
        pgtDTO.MaKH = txtKhachHang.Text
        pgtDTO.NgayGoi = dtpNgayMoSo.Value
        pgtDTO.SoTienGoi = txtSoTienGoi.Text
        Dim tsBUS As New ThamSoBUS()
        Dim tsDTO As New ThamSoDTO()
        tsDTO = tsBUS.LayBangThamSo()
        Dim kt As Boolean = tsBUS.KiemTraSoTienGoiToiThieu(temp, tsDTO.SoTienGoiToiThieu)
        If kt = False Then
            MessageBox.Show("Số tiền gởi tối thiểu là : " & tsDTO.SoTienGoiToiThieu.ToString())
            Return
        End If

        Dim kq As Integer = pgtBUS.ThemPhieuGoiTien(pgtDTO)
        If kq = 1 Then
            Dim stkBUS As New SoTietKiemBUS()
            kq = stkBUS.SoDuTaiKhoanSauKhiGoi(pgtDTO.MaSo, pgtDTO.SoTienGoi)
            If kq = 1 Then
                MessageBox.Show("Gởi thành công")
                txtMaPhieuGoi.Text = (pgtBUS.LayMaPhieuGoiMax() + 1).ToString()
            Else
                MessageBox.Show("gởi thất bại")
            End If
        Else
            MessageBox.Show("gởi thất bại")
            Return
        End If

    End Sub

    Private Sub cbbSoTietKiem_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbbSoTietKiem.SelectedIndexChanged
        Dim maso As Integer = cbbSoTietKiem.SelectedValue
        Dim khBUS As New KhachHangBUS()
        Dim khDTO As New KhachHangDTO()
        khDTO = khBUS.LayKhachHangTheoMaSo(maso)
        txtKhachHang.Text = khDTO.MaKH.ToString()
    End Sub

    Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

    End Sub
End Class
